﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataModel.Models;

namespace DataModel.Managers
{
    public class LessonManager
    {
        readonly UniversityPortalEntities _entityModel;

        public LessonManager()
        {
            _entityModel = new UniversityPortalEntities();
        }

        public void UpdateLesson(LessonRequest lessonRequest)
        {
            var lesson = _entityModel.Lessons.FirstOrDefault(les => les.ID == lessonRequest.Lessonid);

            if (lesson != null)
            {
                var ph = new SubManage.PeriodicityHelper();
                lesson.auditoryID = lessonRequest.Auditoryid;
                lesson.beginDate = lessonRequest.Begindate;
                lesson.beginTime = lessonRequest.BeginTime.TimeOfDay;
                lesson.endDate = lessonRequest.Enddate;
                lesson.endTime = lessonRequest.EndTime.TimeOfDay;
                lesson.lessonTypeID = lessonRequest.Lessonypeid;
                lesson.periodicity = lessonRequest.periodicity;
                lesson.periodicityMask = ph.GetIntMask(lessonRequest.Periodicitymask);
                lesson.professorID = lessonRequest.Professorid;
                lesson.semester = lessonRequest.Semester;
                lesson.subjectID = lessonRequest.Subjectid;
                lesson.weekDay = lessonRequest.Weekday;
            }
            else
            {
                AddLesson(lessonRequest);
            }

            _entityModel.SaveChanges();
        }

        public void AddLesson(LessonRequest lessonRequest)
        {
            if (lessonRequest == null) return;
            //var groups = _entityModel.Groups.Where(g => g.ID == lessonRequest.GroupId);

            var groups = new List<Group>();
            foreach (var groupId in lessonRequest.GroupId)
            {
                Guid id = groupId;
                var group = _entityModel.Groups.Where(g => g.ID == id);
                groups.AddRange(group);
            }
            var lesson = RequestToLesson(lessonRequest);
            foreach (var group in groups)
            {
                lesson.Groups.Add(group);
            }
            _entityModel.AddToLessons(lesson);
            _entityModel.SaveChanges();
        }

        public Lesson RequestToLesson(LessonRequest lessonRequest)
        {
            if (lessonRequest == null) { return null; }
            var ph = new SubManage.PeriodicityHelper();
            return new Lesson
                {
                    ID = lessonRequest.Lessonid,
                    auditoryID = lessonRequest.Auditoryid,
                    beginDate = lessonRequest.Begindate,
                    beginTime = lessonRequest.BeginTime.TimeOfDay,
                    endDate = lessonRequest.Enddate,
                    endTime = lessonRequest.EndTime.TimeOfDay,
                    lessonTypeID = lessonRequest.Lessonypeid,
                    periodicity = lessonRequest.periodicity,
                    periodicityMask = ph.GetIntMask(lessonRequest.Periodicitymask),
                    professorID = lessonRequest.Professorid,
                    semester = lessonRequest.Semester,
                    subjectID = lessonRequest.Subjectid,
                    weekDay = lessonRequest.Weekday
                };
        }


        public void UpdateLesson(LessonModel lessonModel)
        {
            Lesson lesson = _entityModel.Lessons.FirstOrDefault(les => les.ID == lessonModel.LessonID);

            //lesson.auditoryID = lessonModel.auditoryid;
            if (lesson != null)
            {
                lesson.beginDate = lessonModel.BeginDate;
                lesson.beginTime = lessonModel.BeginTime.TimeOfDay;
                lesson.endDate = lessonModel.EndDate;
                lesson.endTime = lessonModel.EndTime.TimeOfDay;
                //lesson.lessonTypeID = lessonModel.lessonypeid;
                lesson.periodicity = lessonModel.Periodicity;
                lesson.periodicityMask = lessonModel.PeriodicityMask;
                //lesson.professorID = lessonModel.professorid;
                lesson.semester = lessonModel.Semester;
                //lesson.subjectID = lessonModel.subjectid;
                lesson.weekDay = lessonModel.WeekDay;
            }

            _entityModel.SaveChanges();
        }

        public LessonModel GetLesson(Guid lessonID)
        {
            LessonModel lessonModel = null;
            var lesson = _entityModel.Lessons.FirstOrDefault(les => les.ID == lessonID);
            if (lesson != null)
            {
                lessonModel = new LessonModel { Auditory = lesson.Auditory, BeginDate = lesson.beginDate, BeginTime = new DateTime(lesson.beginTime.Ticks), EndDate = lesson.endDate, EndTime = new DateTime(lesson.endTime.Ticks), LessonID = lesson.ID, LessonType = lesson.LessonType, Periodicity = lesson.periodicity, PeriodicityMask = lesson.periodicityMask, Professor = lesson.Professor, Semester = lesson.semester, Subject = lesson.Subject, WeekDay = lesson.weekDay };
            }
            return lessonModel;
        }

        public IEnumerable<LessonTypeModel> GetLessonTypes()
        {
            var lessonTypes = _entityModel.LessonTypes.Select(t => new LessonTypeModel
                {
                    ID = t.ID,
                    Name = t.name
                }).AsEnumerable();

            return lessonTypes;
        }

        public Guid[] GetGroupsOnLesson(Guid lessonId)
        {
            var groupsId = new List<Guid>();
            Lesson lesson = _entityModel.Lessons.FirstOrDefault(item => item.ID == lessonId);
            if (lesson != null)
            {
                foreach (var group in lesson.Groups)
                {
                    groupsId.Add(@group.ID);
                }
            }
            return groupsId.ToArray();
        }
    }
}
